package com.huawei.admins.platform.gateway.core.server.reactor;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import reactor.netty.tcp.TcpServer;

public class ReactorNettyServer {

    static final String REMOTE_HOST = System.getProperty("remoteHost", "tomcat");
    static final int LOCAL_PORT = Integer.parseInt(System.getProperty("localPort", "9090"));
    static final int REMOTE_PORT = Integer.parseInt(System.getProperty("remotePort", "8080"));

    public static void main(String[] args) {

        System.err.println("Proxying *:" + LOCAL_PORT + " to " + REMOTE_HOST + ':' + REMOTE_PORT + " ...");
        TcpServer.create()
                .port(LOCAL_PORT)
                .doOnConnection(connection -> {

                    Channel frontendChannel = connection.channel();

                    ChannelFuture backendChannelFuture = new Bootstrap()
                            .group(frontendChannel.eventLoop())
                            .channel(frontendChannel.getClass())
                            .remoteAddress(REMOTE_HOST, REMOTE_PORT)
                            .option(ChannelOption.AUTO_READ, false)
                            .handler(new ProxyBackendHandler(frontendChannel))
                            .connect();

                    Channel backendChannel = backendChannelFuture.channel();
                    backendChannelFuture.addListener(future -> {
                        if (future.isSuccess()) {
                            frontendChannel.read();
                        } else {
                            frontendChannel.close();
                        }
                    });

                    connection.addHandlerLast(new ProxyFrontendHandler(REMOTE_HOST, REMOTE_PORT, backendChannel));
                })
                .bindNow()
                .onDispose()
                .block()
        ;

    }
}
